"""
<name>Solution Rep</name>
<description>Solution Representation</description>
<contact>Leidy Garzon</contact>
<icon>icons/Encoder.svg</icon>
<priority>1030</priority>

"""
from Goldenberry.widgets import *
from Goldenberry.widgets import binary_encoders
from Goldenberry.widgets import real_encoders
from Goldenberry.widgets import OWWidget, inspect, load_widget_ui, OWGUI, QtCore, QDoubleValidator

class GbSolutionRepWidget(OWWidget):
    """Widget for solution representation"""
    range_min = 0.0
    range_max = 1.0
    encoders = {}
    encod_index = [0]
    solution_domain_index = 0
    solution_domains = []
    box = None
    
    def __init__(self, parent=None, signalManager=None):
        OWWidget.__init__(self, parent,signalManager, title='Solution Representation')
        # add a new domain by specifying it in this vector
        self.solution_domains = ["Binary","Real"]
        self.encoders = self.__setup_encoders_list(None)
        self.box = OWGUI.widgetBox(self.controlArea,orientation=0)
        self.setup_interfaces()
        self.setup_ui()
                
    def setup_interfaces(self):
        self.outputs = [("Solution Rep", SolutionRep)]

    def setup_ui(self):
        load_widget_ui(self)
        solution_domain_listbox = OWGUI.comboBox(self, self, "solution_domain_index", items=self.solution_domains, tooltip="Select the domain of the solution", callback = self.set_solution_domain)
        encod_listbox = OWGUI.listBox(self, self, "encod_index", "encoders", tooltip="Select the solution encode", callback = self.set_encoder)
        range_min_listbox = OWGUI.lineEdit(self.box, self, "range_min", label="Min:", valueType = float)#, validator = QDoubleValidator(0.0, 1.0, 4, self.controlArea))
        range_max_listbox = OWGUI.lineEdit(self.box, self, "range_max", label="Max:", valueType = float)#, validator = QDoubleValidator(0.0, 1.0, 4, self.controlArea))
        self.box.setEnabled(False)
        self.paramBox.setLayout(QFormLayout(self.paramBox))
        self.paramBox.layout().addRow('Solution Domain', solution_domain_listbox) 
        self.paramBox.layout().addRow('Encode Function', encod_listbox)
        self.paramBox.layout().addRow("Range", self.box)
        #Subscribe to signals
        QObject.connect(self.applyButton,QtCore.SIGNAL("clicked()"), self.apply)

    def set_solution_domain(self):
        self.encoders = self.__setup_encoders_list(self.solution_domain_index)

    def set_encoder(self):
        pass

    def apply(self):
        solution_rep = SolutionRep(self.encoders.values()[self.encod_index[0]].__name__, self.range_min, self.range_max)
        solution_rep.set_range_min(self.range_min)
        solution_rep.set_range_max(self.range_max)
        solution_rep.set_solution_domain(self.solution_domains[self.solution_domain_index])
        solution_rep.set_encode_function(self.encoders.values()[self.encod_index[0]].__name__)
        self.accept()
        self.send("Solution Rep", solution_rep)

    def __setup_encoders_list(self, domain = 0):
        if domain is not None:
            if domain == 1:
                encoder_list = dict(inspect.getmembers(real_encoders, lambda member: inspect.isclass(member)))
                self.box.setEnabled(True)
            else:
                encoder_list = dict(inspect.getmembers(binary_encoders, lambda member: inspect.isclass(member)))
                self.box.setEnabled(False)
        else: 
            encoder_list = dict(inspect.getmembers(binary_encoders, lambda member: inspect.isclass(member)))
        del encoder_list['Encoder']
        return encoder_list
        
    if __name__=="__main__":
        test_widget()

    def test_widget():
        app = QApplication(sys.argv)
        w = GbSolutionRepWidget()
        w.show()
        app.exec_()
